package com.dp.appi.service.impl;

import com.dp.appi.dao.ExpCodesetDao;
import com.dp.appi.dao.ExplorationDao;
import com.dp.appi.exception.ExplorationRuntimeException;
import com.dp.appi.service.ExplorationService;
import com.dp.appi.util.RegularUtil;
import com.dp.appi.util.SYS;
import com.dp.appi.util.StringUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

/**
 * Created by xiaoji on 2016/5/9.
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ExplorationServiceImpl implements ExplorationService {
    @Autowired
    private ExplorationDao explorationDao;

    @Autowired
    private ExpCodesetDao expCodesetDao;

    /**
     * 获取踏勘点列表
     *
     * @param exploration_type 踏勘类型(新建11 / 拆旧12)
     * @param stage_id         项目阶段id
     * @param id 当前用户id
     * @return
     */
    @Override
    public List<Map<String, Object>> explorationList(String exploration_type, Long stage_id,String id,String explor_id) {
        int classfy = explorationDao.getClassify(stage_id,id);
        Long explorId = null;

        if(null!=explor_id&&!"".equals(String.valueOf(explor_id))){
                 explorId = Long.parseLong(explor_id);
        }
        Integer explorationType = null;
        if(null!=exploration_type&&!"".equals(String.valueOf(exploration_type))){
            explorationType = Integer.parseInt(exploration_type);
        }
        return explorationDao.explorationList(explorationType,stage_id,classfy,explorId);
    }

    @Override
    public List<Map<String, Object>> explorationList_high(String exploration_type, Long stage_id, String id) {
        int classfy = explorationDao.getClassify(stage_id,id);
        return explorationDao.explorationList_high(exploration_type,stage_id,classfy);
    }

    /**
     * 删除踏勘点
     *
     * @param exploration_id 踏勘点id
     */
    @Override
    public void delExplor(Long exploration_id,String id) {
        boolean boo = explorationDao.isDelExplor(exploration_id,id);
        if(boo){
            explorationDao.delExplor(exploration_id);
        }else{
            throw new ExplorationRuntimeException(SYS.EXPO001.name());
        }
    }


    /**
     * 判断特征点是否存在
     *
     * @param feature_id
     * @param ptType
     * @return
     */
    @Override
    public boolean isExistFeature(Long feature_id,int ptType) {
        return  explorationDao.isExistFeature(feature_id,ptType);
    }

    /**
     * 获取踏勘点信息【app端获取点位信息】
     *
     * @param exploration_id
     * @return
     */
    @Override
    public Map<String, Object> explorInfo(Long exploration_id) {
        Map<String,Object> result = explorationDao.explorInfo(exploration_id);//点位的结果集
        //
        result.get("distance");
        result.get("span");

        if(result!=null&&null!=result.get("pt_objectid")){
            //pt_objectid：定位点对象id
            result.put("pt",explorationDao.pointInfo(Long.parseLong(String.valueOf(result.get("pt_objectid"))),
                                                     Long.parseLong(String.valueOf(result.get("pt_id")))));
        }


        return result;
    }

    /**
     * 获取踏勘点信息
     *
     * @param exploration_id
     * @return
     */
    @Override
    public Map<String, Object> explorBaseInfo(Long exploration_id) {
        Map<String,Object> result = explorationDao.explorBaseInfo(exploration_id);
        return result;
    }

    /**
     * 通过通道id获取特征点信息
     *
     * @param channelID
     * @return
     */
    @Override
    public Map<String, Object> explorBaseInfoByChannelID(Long channelID) {
        Map<String,Object> result = explorationDao.explorBaseInfoByChannelID(channelID);
        return result;
    }

    /**
     * 踏勘点信息设置
     *
     * @param param 踏勘点参数
     * @param id    当前用户id
     */
    @Override
    public Long addExploration(Map<String, String> param, String id) {
        String pt = param.get("pt");
        Map<String,String> param2 = new Gson().fromJson(pt,new TypeToken<Map<String,String>>(){}.getType());
        int classify = explorationDao.getClassify(Long.parseLong(param.get("stage_id")),id);
        Long pt_objectid=null;
        long pt_id = Long.parseLong(param.get("pt_id"));
        if(pt_id == 100001){//搭火点
            pt_objectid = explorationDao.addWiring(param2);
        }else if(pt_id == 100002){//柱上变压器
            pt_objectid = explorationDao.addTransformer(param2);
        }else if(pt_id == 100004){//杆塔
            {
                String voltage_grade = param.get("voltage_grade");
                pt_objectid = explorationDao.addPole(param2,voltage_grade);

                //更新共杆点位属性为是
                String g_exp_id = param.get("g_exp_id");
                if(!g_exp_id.equals("")){
                    Long ptid = explorationDao.update_or_save_pole(Long.valueOf(g_exp_id));
                    param.put("pt_objectid",String.valueOf(ptid));
                }

            }
        }else if(pt_id == 100005){//下户点
            pt_objectid = explorationDao.addUserPoint(param2);
        }else if(pt_id == 100006){//户表箱
            pt_objectid = explorationDao.addUserElecbox(param2);
        }else if(pt_id == 100014){//植被
            pt_objectid = explorationDao.addVegetation(param2);
        }else if(pt_id == 100015){//房屋
            pt_objectid = explorationDao.addHouses(param2);
        }else if(pt_id == 100016){//通讯线
            pt_objectid = explorationDao.addCommLine(param2);
        }else if(pt_id == 100017){//低压线路
            pt_objectid = explorationDao.addLowLine(param2);
        }else if(pt_id == 100018){//沟河流
            pt_objectid = explorationDao.addRivers(param2);
        }else if(pt_id == 100019){//道路
            pt_objectid = explorationDao.addRoad(param2);
        }else if(pt_id == 100020){//高压线路
            pt_objectid = explorationDao.addHighLine(param2);
        }else{
            //throw new ExplorationRuntimeException(SYS.EXPO002.name());
        }
        if(pt_objectid==null){
            param.put("pt_objectid",null);
        }else{
            param.put("pt_objectid",String.valueOf(pt_objectid));
        }
        param.put("classify",String.valueOf(classify));//数据类型
        if(!StringUtils.isEmpty(param.get("distance")) && !StringUtils.isEmpty(param2.get("span"))){
            param.put("distance",param2.get("span"));
        }
        Long key = explorationDao.addExploration(param, id);

        String stage_id = param.get("stage_id");
        String current_num = param.get("number");
        explorationDao.updateNum(current_num,stage_id ,pt_id);
        return key;
    }

    @Override
    public Long addBj(Map<String, String> param, String id) {
        return explorationDao.addBj(param);
    }

    /**
     * 获取定位点上级定位点
     *
     * @param exploration_id 获取踏勘点id
     */
    @Override
    public Map<String,Object> beforeExp(Long exploration_id) {
        return explorationDao.beforeExp(exploration_id);
    }

    /**
     * 判断特征点是否已经关联
     *
     * @param featureId
     * @return
     */
    @Override
    public boolean isCorrFeature(Long featureId) {
        return  explorationDao.isCorrFeature(featureId);
    }

    /**
     * 更新踏勘典设
     *
     * @param codesetId
     * @param explorationId
     */
    @Override
    public void upCodeSet(Long codesetId, Long explorationId) {
        explorationDao.upCodeSet(codesetId,explorationId);
        expCodesetDao.updateExpCodeset(explorationId);
    }

    /**
     * 获取台区
     *
     * @param stage_id
     * @return
     */
    @Override
    public List<Map<String, Object>> transformerList(Long stage_id,String id) {
        int classfy = explorationDao.getClassify(stage_id,id);
        return explorationDao.transformerList(stage_id,classfy);
    }

    /**
     * number、 before_exp、exploration_type、pt_id、lng、lat、corner、alt、distance、location_way
     *
     * @param
     * @param
     */

    /** 修改杆塔信息上联，同时修改交叉跨院的上联
     *
     * @param exploration_id
     * @param new_before_exp_id
     */
    @Override
    public void upjckyExploration(Long exploration_id, Long new_before_exp_id) {
        explorationDao.upjckyExploration(exploration_id,new_before_exp_id);

    }

    /**
     *  【app端点击点位修改信息】
     */

        @Override
    public void upExploration(Map<String, Object> param, Long explorationId) {
        String pt = String.valueOf(param.get("pt"));
        Map<String,String> param2 = new Gson().fromJson(pt,new TypeToken<Map<String,String>>(){}.getType());
        Map codiMap = new HashMap();
        codiMap.put("exploration_id",explorationId);


        //设置台区
        if(Long.valueOf(String.valueOf(param.get("pt_id")))==100002){
            param.put("explor_id",param.get("exploration_id"));
        }else {
            Object beforE = param.get("before_exp");//上联ID
            Long tempExplorId = null;
            if(StringUtil.isNotEmpty(beforE)){
                tempExplorId = explorationDao.getExplorIdByBeforeExp(Long.parseLong(String.valueOf(beforE)),new ArrayList<Long>());
            }
            param.put("explor_id",tempExplorId);
        }

//        上联点位id，如果上联的点位没有改变，就不更新上联角度
        Object new_before_exp_id = param.get("before_exp");
        Object old_before_exp_id = param.get("old_before_exp_id");
        if(!String.valueOf(new_before_exp_id).equals(String.valueOf(old_before_exp_id))){
                explorationDao.upBeforeExpCorner(new_before_exp_id, param.get("corner"));//新的上联角度
        }
        if(!StringUtils.isEmpty(param.get("distance")) && !StringUtils.isEmpty(param2.get("span"))){
            param.put("distance",param2.get("span"));
        }
        param.remove("corner");

       //更新点位的具体属性值，除开corner
        explorationDao.upCommField(param,"p_exploration",codiMap);

    }

    @Override
    public Double returnCorner(Long exploration_id,double lng, double lat, Long befor_id,String type) {
        return explorationDao.returnCorner(exploration_id,lng,lat,befor_id,type);
    }

    @Override
    public Map<String, Object> exp_obj(double lng, double lat, Long befor_id) {
        return explorationDao.exp_obj(lng,lat,befor_id);
    }

    //点位重新选择上联后下联中所有点位的台区标识更新
    @Override
    public void updateExp_id(Long exploration_id, Long before_exp_id) {
        explorationDao.updateExp_id(exploration_id,before_exp_id);
    }

    @Override
    public Integer getClassify(Long stage_id, String id) {
        return explorationDao.getClassify(stage_id,id);
    }

    /**
     * 修改通道特征点距离上级距离
     *
     * @param exploration_id
     * @param distance
     */
    @Override
    public void upExpDistance(Long exploration_id, Float distance) {
        explorationDao.upExpDistance(exploration_id,distance);
    }

}
